﻿//     Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using GatewayTests.Utils;
using Newtonsoft.Json;

namespace GatewayTests.Models
{
    public class UrlTest
    {
        private string _value;
        private string _expectedValue;

        [Required]
        public string Value 
        {
            get
            {
                return ParseString(_value);
            }
            set { _value = value; }
        }

        [Required]
        public string ExpectedValue
        {
            get
            {
                return ParseString(_expectedValue);
            }
            set { _expectedValue = value; }
        }

        public string Comments { get; set; }

        private static string ParseString(string val)
        {
            var request = HttpContext.Current.Request;

            var gatewayServerDomain = request["ORIGINAL_HTTP_HOST"]; //TODO: need to pass this through from the gateway
            var webServerDomain = request["HTTP_HOST"].Split(':')[0];
            
            var gatewaySubdir = "";
            var urlEncodedGatewaySubdir = "";
            if (request["SCRIPT_NAME"].Split('/').Count() > 1)
            {
                gatewaySubdir = (request["SCRIPT_NAME"].Split('/'))[0]; //TODO: need to pass this through from the gateway
            }

            if (string.IsNullOrEmpty(gatewayServerDomain))
            {
                gatewayServerDomain = "GatewayServerDomain_NotSet";
            }

            //override with querystring values
            webServerDomain = GetFromQueryString(webServerDomain, "WebServerDomain");
            gatewayServerDomain = GetFromQueryString(gatewayServerDomain, "GatewayServerDomain");
            gatewaySubdir = GetFromQueryString(gatewaySubdir, "GatewaySubdir");
            urlEncodedGatewaySubdir = HttpContext.Current.Server.UrlEncode(gatewaySubdir);

            val = Regex.Replace(val, "{GatewayServerDomain}", gatewayServerDomain, RegexOptions.IgnoreCase);
            val = Regex.Replace(val, "{WebServerDomain}", webServerDomain, RegexOptions.IgnoreCase);
            val = Regex.Replace(val, "{GatewaySubdir}", gatewaySubdir, RegexOptions.IgnoreCase);
            if (urlEncodedGatewaySubdir != null)
            {
                val = Regex.Replace(val, "{UrlEncodedGatewaySubdir}", urlEncodedGatewaySubdir, RegexOptions.IgnoreCase);
            }

            return val;
        }

        private static string GetFromQueryString(string gatewayServerDomain, string querystringName)
        {
            var request = HttpContext.Current.Request;

            if (!string.IsNullOrEmpty(request.QueryString[querystringName]))
            {
                gatewayServerDomain = request.QueryString[querystringName];
            }
            return gatewayServerDomain;
        }
    }


}
